#import "ALElementBarDetailsBackgroundView.h"
#import "NSShadow+MCAdditions.h"
#import "NSBezierPath+SMRAdditions.h"


#pragma mark Constants
static const CGFloat ALElementBarDetailsBackgroundCornerRadius = 6.0f;
static const CGFloat ALElementBarDetailsBackgroundInnerShadowBlurRadius = 15.0f;
static const CGFloat ALElementBarDetailsBackgroundUpperShadowOffset = 5.0f;
static const CGFloat ALElementBarDetailsBackgroundUpperShadowBlurRadius = 10.0f;


#pragma mark -
#pragma mark Class variables

static NSGradient *ALElementBarDetailsBackgroundGradient;
static NSShadow *ALElementBarDetailsBackgroundInnerShadow;
static NSShadow *ALElementBarDetailsBackgroundUpperShadow;
static NSColor *ALElementBarDetailsBackgroundBorderColor;


#pragma mark -
@interface ALElementBarDetailsBackgroundView(ALElementBarDetailsBackgroundViewPrivateMethods)
-(NSBezierPath *)framePathForRect_:(NSRect)frameRect;
@end


#pragma mark -
@implementation ALElementBarDetailsBackgroundView

+(void)initialize {
	NSColor *topColor = [NSColor colorWithCalibratedWhite:0.55 alpha:1.000];
	NSColor *bottomColor = [NSColor colorWithCalibratedWhite:0.35 alpha:1.000];
	ALElementBarDetailsBackgroundGradient = [[NSGradient alloc] initWithStartingColor:topColor endingColor:bottomColor];
	
	NSColor *innerShadowColor = [NSColor colorWithCalibratedWhite:0.2 alpha:0.4];
	ALElementBarDetailsBackgroundInnerShadow = [[NSShadow alloc] mcInitWithColor:innerShadowColor offset:NSZeroSize blurRadius:ALElementBarDetailsBackgroundInnerShadowBlurRadius];
	
	NSColor *upperShadowColor = [NSColor colorWithCalibratedWhite:0 alpha:0.35];
	NSSize upperShadowOffset = NSMakeSize(0, -ALElementBarDetailsBackgroundUpperShadowOffset);
	ALElementBarDetailsBackgroundUpperShadow = [[NSShadow alloc] mcInitWithColor:upperShadowColor offset:upperShadowOffset blurRadius:ALElementBarDetailsBackgroundUpperShadowBlurRadius];
	
	ALElementBarDetailsBackgroundBorderColor = [NSColor colorWithCalibratedWhite:0 alpha:0.75];
}

-(void)drawRect:(NSRect)dirtyRect {
	NSBezierPath *framePath = [self framePathForRect_:[self bounds]];
	[ALElementBarDetailsBackgroundGradient drawInBezierPath:framePath angle:270];
	[framePath smrFillWithInnerShadow:ALElementBarDetailsBackgroundInnerShadow];
	[framePath smrFillWithInnerShadow:ALElementBarDetailsBackgroundUpperShadow];
	[ALElementBarDetailsBackgroundBorderColor setStroke];
	[framePath stroke];
}

-(NSBezierPath *)framePathForRect_:(NSRect)frameRect {
	CGFloat minx = NSMinX(frameRect);
	CGFloat maxx = NSMaxX(frameRect);
	CGFloat miny = NSMinY(frameRect);
	CGFloat maxy = NSMaxY(frameRect);
	CGFloat corner = ALElementBarDetailsBackgroundCornerRadius;
	NSBezierPath *framePath = [NSBezierPath bezierPath];
	[framePath moveToPoint:NSMakePoint(minx, maxy)];
	[framePath lineToPoint:NSMakePoint(maxx, maxy)];
	[framePath lineToPoint:NSMakePoint(maxx, miny + corner)];
	[framePath appendBezierPathWithArcFromPoint:NSMakePoint(maxx, miny) toPoint:NSMakePoint(maxx - corner, miny) radius:corner];
	[framePath lineToPoint:NSMakePoint(minx + corner, miny)];
	[framePath appendBezierPathWithArcFromPoint:NSMakePoint(minx, miny) toPoint:NSMakePoint(minx, miny + corner) radius:corner];
	[framePath closePath];
	return framePath;
}

@end
